﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.IO;

namespace BattleforgeAssault
{
    /// <summary>
    /// A class for holding global static helper methods.
    /// </summary>
    public static class Util
    {
        /// <summary>
        /// Creates and starts an animation of a double property.
        /// </summary>
        /// <param name="from">Starting value.</param>
        /// <param name="to">Ending value.</param>
        /// <param name="duration">The duration in sec.</param>
        /// <param name="target">The target object.</param>
        /// <param name="prop">The property to animate.</param>
        public static Storyboard PlayDoubleAnimation(double from, double to, double duration, DependencyObject target, DependencyProperty prop)
        {
            Storyboard story = new Storyboard() { Duration = TimeSpan.FromSeconds(duration) };
            DoubleAnimation animation = new DoubleAnimation()
            {
                From = from,
                To = to,
                Duration = TimeSpan.FromSeconds(duration),
                EasingFunction = new CircleEase()
            };
            Storyboard.SetTarget(animation, target);
            Storyboard.SetTargetProperty(animation, new PropertyPath(prop));
            story.Children.Add(animation);
            story.Begin();
            return story;
        }

        /// <summary>
        /// Finds an image resource, first looking for it locally, then on the web, then just using a program resource.
        /// </summary>
        /// <param name="localName">The local filename of the image (must be in MyDocuments folder).</param>
        /// <param name="remoteName">The remote path to the image (formatted as a uri).</param>
        /// <param name="resourceName">Name of the resource to use as a final resort.</param>
        /// <returns>A BitmapImage with the source set to one of the inputs.</returns>
        public static BitmapImage FindResource(string localName, string remoteName, string resourceName)
        {
            // assume we will have to use the remote resource
            Uri location = new Uri(remoteName, UriKind.Absolute); ;

            if (Application.Current.HasElevatedPermissions)
            {
                string localPath = System.IO.Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), localName).
                    Replace("\\", "/");

                if (File.Exists(localPath)) { location = new Uri(localPath, UriKind.Absolute); }
            }

            var bitmap = new BitmapImage(location);

            // handle the case where loading the remote resource failed for any reason
            bitmap.ImageFailed += (s, e) =>
            {
                var image = Application.GetResourceStream(
                    new Uri("BattleforgeAssault;component/Resources/" + resourceName, UriKind.Relative));
                bitmap.SetSource(image.Stream);
            };

            return bitmap;
        }
    } // Class
} // Namespace
